<?php

class HoiDapAmway_IndexController extends BaseController
{
	
	public function __construct()
	{
		parent::__construct();
		$this->_children[] = new Request('trang-chu/index/nav-header',array('hoi-dap-amway'));
	}
	
	public function indexAction() 
	{
	    $this->filterAction();
	}
	
	public function filterAction($cat_id = NULL)
	{
		$objCategory = new Amway_Category();
		if (empty($cat_id)) 
		{
			$rowCategory = $objCategory->getRow("cat_default = 1 && active = 1");
			$this->oView->rowCategory = $rowCategory;
			$cat_id = $rowCategory['id']; 
		} else 
		{
			$rowCategory = $objCategory->getRow("id = ? && active = 1",array($cat_id));
			$this->oView->rowCategory = $rowCategory;
		}
		
		$this->oView->cat_id = $cat_id;
		$this->oView->current_member_id = h($_SESSION["fb_user"]['id']);//1;
		$this->oView->current_link = site_url('hoi-dap-amway/index/filter/'.$cat_id);		
		$this->oView->link_add_url = site_url('hoi-dap-amway/index/add-question');
		$this->oView->link_add_comment_url = site_url('hoi-dap-amway/index/add-comment');
		
		$objQuestion = new Amway_Question();
		$rsQuestion = $objQuestion->getRowset("cat_id = ? && active = ?",array($cat_id, 1),"last_update DESC");
		$this->oView->rsQuestion = $rsQuestion;

		$this->renderView('hoi-dap-amway/index/index');
	}

	public function addQuestionAction()
	{
		if ($this->oParams->isPost())
		{
			$objQuestion = new Amway_Question();
			
			$cat_id = $this->oParams->varPost("cat_id");
			$data["cat_id"] = $cat_id;
			$data["member_id"] = $this->oParams->varPost("member_id");
			$data["question"] = $this->oParams->varPost("question");
				
			$data['active'] = 0;
			$data['create_at'] = now_to_mysql();
			$last_id = $objQuestion->insert($data);
			
			// Send notify email
// 			$this->send_notify_email($data);
			
			echo json_encode(array("message" => "Cảm ơn bạn đã gửi thông tin cho Amway! Thông tin của bạn đang được chúng tôi xử lý và sẽ phản hồi cho bạn trong thời gian sớm nhất."));
			exit();			
				
// 			redirect('hoi-dap-amway/index/filter/'.$cat_id);
		}
		
		redirect('hoi-dap-amway/index/index');
	}	
	
	public function addCommentAction()
	{
		if ($this->oParams->isPost())
		{
			$objComment = new Amway_Comment();
			
			$cat_id = $this->oParams->varPost("cat_id");
	
			$data["question_id"] = $this->oParams->varPost("question_id");
			$data["member_id"] = $this->oParams->varPost("member_id");
			$data["content"] = $this->oParams->varPost("content");
	
			$data['active'] = 0;
			$data['create_at'] = now_to_mysql();
			$last_id = $objComment->insert($data);
			
			echo json_encode(array("message" => "Cảm ơn bạn đã gửi thông tin cho Amway! Thông tin của bạn đang được chúng tôi xử lý và sẽ phản hồi cho bạn trong thời gian sớm nhất."));
			exit();
	
// 			redirect('hoi-dap-amway/index/filter/'.$cat_id);
		}
	
		redirect('hoi-dap-amway/index/index');
	}

	public function loadMoreCommentAction($question_id = NULL, $from_offset = 0)
	{
		if (empty($question_id ))
			return "";

		$objComment = new Amway_Comment();
		$rsComment = $objComment->getAllCommentOfQuestion($question_id ,TRUE, $from_offset);
		
// 		echo "<pre>";
// 		print_r($objComment->getAllCommentOfQuestion($question_id ,TRUE, 0, 2));
// 		echo "</pre>";
		
// 		echo "----------------------------";
		
// 		echo "<pre>";
// 		print_r($objComment->getAllCommentOfQuestion($question_id ,TRUE, 2));
// 		echo "</pre>";
		
		
// 		echo "<pre>";
// 		print_r($rsComment);
// 		echo "</pre>";
// 		exit();
		
// 		exit();
		
		$this->oView->rsComment = $rsComment;
		
		echo $this->oView->fetch('hoi-dap-amway/index/_render_comment');
		exit();
	}
	

	private function send_notify_email($rowQuestion)
	{
		$arrVal['rowQuestion'] = $rowQuestion;
		$arrVal['fb_user'] = $_SESSION["fb_user"];
		
		$oConfigSys = new Base_ConfigureSystem();
		$rs = $oConfigSys->getGroupConfigure(2);
		
		foreach ($rs as $row)
			$configEmail[$row['code']] = $row['value'];
		
		$email_view = new View();
		$body = $email_view->parser(__LAYOUT_PATH."/email_template/notify_question.phtml",$arrVal);
		
		$email = new Email($this->oConfigureSystem['email_contact'],"Câu hỏi mới",$body);
		$email->connect($configEmail);
		//		$result = @$email->Send();
		$result = $email->Send();		
	}
	
	public function emailAction()
	{
		$arrVal['rowQuestion'] = array("content" => "noi dung gui di");
		$arrVal['fb_user'] = array("fullname" => "tien duc", "email" => "tienduc@yahoo.com");		
		
		$oConfigSys = new Base_ConfigureSystem();
		$rs = $oConfigSys->getGroupConfigure(2);
		
		foreach ($rs as $row)
		{
			$configEmail[$row['code']] = $row['value'];
		}
		
		
		
echo "<pre>";
print_r($configEmail);
echo "</pre>";

echo "<pre>";
print_r($this->oConfigureSystem['email_contact']);
echo "</pre>";
		
		$email_view = new View();
		$body = $email_view->parser(__LAYOUT_PATH."/email_template/notify_question.phtml",$arrVal);
		
		$email = new Email($this->oConfigureSystem['email_contact'],"Câu hỏi mới",$body);
		$email->connect($configEmail);
		//		$result = @$email->Send();
		$result = $email->Send();		
		
		echo "<pre>";
		print_r($result);
		echo "</pre>";
		exit();
		
	}
	
}
